<template>
  <div class="app-container dict-holidays-list-wrap">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">

      <el-form-item label="说明" prop="description">
        <el-input v-model="queryParams.description" placeholder="" clearable />
      </el-form-item>
      <el-form-item label="查询日期" prop="day">
        <el-date-picker v-model="queryParams.day" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="clickAddbtn"
          v-hasPermi="['config:holiday:add']">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" size="mini" @click="handleDelete"
          v-hasPermi="['config:holiday:del']">删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" max-height="550" :data="dataList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handelEdit(scope.row)" v-hasPermi="['config:holiday:edit']">编辑
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="说明" prop="description" />
      <el-table-column label="日期" prop="day"></el-table-column>
      <el-table-column label="适用分公司" prop="companyName" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog title="添加假期" :visible.sync="dialogVisible" width="600px">
      <div class="dialog-form-box">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">

          <el-form-item label="说明：" prop="description">
            <el-input v-model="ruleForm.description" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="日期：" prop="day">
            <el-date-picker v-model="ruleForm.day" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
              placeholder="选择日期" style="width: 85%;">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="适用分公司：" prop="deptId">
            <treeselect v-model="ruleForm.deptId" :options="deptList" :normalizer="normalizer" :show-count="true"
              placeholder="请选择" :default-expand-level="1" style="width: 85%;" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    holidayList,
    holidayAdd,
    holidayEdit,
    holidayDelBatch
  } from '@/api/system/dict/holidays.js'
  import {
    listDept
  } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "DictHolidays",
    components: {
      Treeselect
    },
    data() {
      return {
        loading: false,
        multiple: true,
        queryParams: {
          pageNum: 1,
          pageSize: 30
        },
        dialogVisible: false,
        ruleForm: {},
        rules: {
          description: [{
            required: true,
            message: "请输入说明",
            trigger: "change"
          }],
          day: [{
            required: true,
            message: "请选择日期",
            trigger: "change"
          }],
          deptId: [{
            required: true,
            message: "请选择分公司",
            trigger: "change"
          }],
        },
        dataList: [],
        total: 0,
        seletIds: [],
        deptList: []

      };
    },


    created() {
      this.getList()
      this.listDept()
    },
    methods: {
      /** 转换菜单数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children
        };
      },
      //获取所属分行
      listDept() {
        listDept(this.queryParams).then(response => {
          this.deptList = this.handleTree(response.data, "deptId");
        });
      },
      getList() {
        this.loading = true;
        console.log(this.queryParams)
        holidayList(this.queryParams).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false;
        })
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.seletIds = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      //批量删除产品
      handleDelete() {
        let that = this
        let ids = ''
        this.seletIds.forEach((item, i) => {
          if (this.seletIds.length - 1 == i) {
            ids += item
          } else {
            ids += item + ','
          }
        });
        this.$modal.confirm('是否确认删除所选数据项？').then(function () {
          holidayDelBatch({
            ids: ids
          }).then(res => {
            that.getList();
            that.$modal.msgSuccess("删除成功");
          });
        }).catch(function () {});
      },
      // 表单重置
      reset() {
        this.resetForm("ruleForm");
        this.ruleForm = {
          id: '',
          description: '',
          day: new Date(),
          deptId: null
        };
      },
      //点击添加按钮弹出表单
      clickAddbtn() {
        this.dialogVisible = true
        this.reset()
      },
      //点击编辑弹出编辑表单并赋值
      handelEdit(row) {
        this.dialogVisible = true
        this.reset()
        this.ruleForm.id = row.id
        this.ruleForm.description = row.description
        this.ruleForm.day = row.day,
          this.ruleForm.deptId = row.deptId
      },
      //点击提交表单
      handelConfirm() {
        this.$refs["ruleForm"].validate(valid => {
          if (valid) {
            const params = {
              id: this.ruleForm.id || null,
              description: this.ruleForm.description,
              day: this.ruleForm.day,
              deptId: this.ruleForm.deptId
            }
            if (this.ruleForm.id) {
              holidayEdit(params).then(res => {
                this.$modal.msgSuccess("添加成功");
                this.dialogVisible = false;
                this.getList();
              })
            } else {
              holidayAdd(params).then(res => {
                this.$modal.msgSuccess("添加成功");
                this.dialogVisible = false;
                this.getList();
              })
            }
          }
        })
      }

    }
  };

</script>

<style rel="stylesheet/scss" lang="scss">

</style>
